﻿using PlaneClean.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;
using static System.Net.WebRequestMethods;

namespace PlaneClean
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            // Web API 配置和服务
            //使api返回为json 
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            jsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.RoundtripKind;
            jsonFormatter.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
            jsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
            config.MessageHandlers.Add(new CommonMessageHandler());
            config.Filters.Add(new AuthorizeFilter());
            config.Filters.Add(new LoginFilter());
            config.Filters.Add(new AnnotationFilter());
            config.Filters.Add(new ExceptionFilter());
            ////跨域配置
            //var cor = new EnableCorsAttribute("*", "*", "*");
            ////var cor = new EnableCorsAttribute(AuthorizeFilter.url, "*", "*");
            //cor.SupportsCredentials = true;// 允许cookie发送，否则session不可用
            //config.EnableCors(cor);
            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
